package com.wise.site.detail;

import java.util.ArrayList;

import org.taptwo.android.widget.CircleFlowIndicator;
import org.taptwo.android.widget.ViewFlow;
import org.taptwo.android.widget.ViewFlow.OnScrollListener;

import com.wise.site.R;
import com.wise.site.adapter.BusinessAdapter;
import com.wise.site.adapter.BusinessAdapter.BUSINESS_TYPE;
import com.wise.site.adapter.SupplyImageAdapter;
import com.wise.site.model.GoogleMapActivity;
import com.wise.site.model.GoogleMapBrowserActivity;
import com.wise.site.model.MapActivity;
import com.wise.site.protocol.action.ShopDetailAction;
import com.wise.site.protocol.action.ShopListAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.result.BussnissItem;
import com.wise.site.protocol.result.IndexerBarEntry;
import com.wise.site.protocol.result.InfoItem;
import com.wise.site.protocol.result.LoginResult;
import com.wise.site.protocol.result.ShopDetailResult;
import com.wise.site.protocol.result.ShopItem;
import com.wise.site.protocol.result.ShopItemsResult;
import com.wise.site.utils.CollectorUtils;
import com.wise.site.utils.Constanct;
import com.wise.site.utils.DataCache;
import com.wise.site.utils.ImageLoader;
import com.wise.site.utils.CollectorUtils.UploadData;
import com.wise.site.utils.ImageLoader.Imager;
import com.wise.site.widget.HeadlineScrollView;
import com.wise.site.widget.PullRefenceList;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;


public class ShopsDetailsActivity extends Activity implements View.OnClickListener {
	
	private TextView mAbout;
	private TextView mProduct;
	private TextView mBuy;
	private PullRefenceList mSaleListView;
	private PullRefenceList mBuyListView;
	private BusinessAdapter mSaleBusinessAdapter;
	private BusinessAdapter mBuyBusinessAdapter;
	private ShopDetailAction mAction;
	private View imagePanel;
	private ViewFlow mViewFlow;
	private ShopItem mItem;
	private HeadlineScrollView about;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shops_details_activity);
		
		
		char address[] = getString(R.string.address).toCharArray();
		((TextView)findViewById(R.id.shop_range_lab)).setText(address[0] + "　　" + address[1] + " :");
		imagePanel = findViewById(R.id.image_panel);
		mViewFlow = (ViewFlow) imagePanel.findViewById(R.id.view_flow);
		about = (HeadlineScrollView) findViewById(R.id.about);
		
		findViewById(R.id.phone_txt_layout).setOnClickListener(this);
		findViewById(R.id.shop_range_layout).setOnClickListener(this);
		findViewById(R.id.about_txt).setOnClickListener(this);
		findViewById(R.id.my_product_txt).setOnClickListener(this);
		findViewById(R.id.my_buy_txt).setOnClickListener(this);
		findViewById(R.id.title_back).setOnClickListener(this);
		findViewById(R.id.title_share).setOnClickListener(this);
		findViewById(R.id.title_collect).setOnClickListener(this);
		((TextView)findViewById(R.id.activity_title)).setText(getString(R.string.shop_text));
		
		mItem = (ShopItem) getIntent().getSerializableExtra(Constanct.INFO_ENTRY);
		
		if(mItem == null){
			int id = getIntent().getIntExtra(Constanct.INTENT_ID, -1);
			ShopListAction action = new ShopListAction(ACTION_TYPE.ACTION_COMMON, "shopinfo");
			action.addJsonParam("shopid", id);
			action.moveToFristPage();
			action.setActionListener(new ActionListener<ShopItemsResult>() {

				@Override
				public void onSucceed(ShopItemsResult result) {
					if(result.list.size() > 0){
						mItem = result.list.get(0);
						bindData(mItem);
						return;
					}
					
					Toast.makeText(ShopsDetailsActivity.this, "没有找到商铺", Toast.LENGTH_SHORT).show();
				}

				@Override
				public void onError(int resultCode) {
					Toast.makeText(ShopsDetailsActivity.this, "没有找到商铺", Toast.LENGTH_SHORT).show();
				}
			});
			ProtocolManager.getProtocolManager().submitAction(action);
		} else {
			bindData(mItem);
		}
		
		
//		mAction = new ShopDetailAction(ACTION_TYPE.ACTION_COMMON, "getShopsDetails");
//		mAction.setActionListener(new ActionListener<ShopDetailResult>() {
//
//			@Override
//			public void onSucceed(ShopDetailResult result) {
//				// TODO Auto-generated method stub
//				bindData(result);
//			}
//
//			@Override
//			public void onError(int resultCode) {
//				// TODO Auto-generated method stub
//				
//			}
//		});
//		ProtocolManager.getProtocolManager().submitAction(mAction);
	}
	
	private void bindData(ShopItem result){
		((TextView)findViewById(R.id.shop_name)).setText(result.shopName);
		((TextView)findViewById(R.id.shop_range)).setText(result.address);
		((TextView)findViewById(R.id.phone_txt)).setText(result.phone);
		((TextView)findViewById(R.id.shop_snipping)).setText(result.description);
		ImageLoader.getInstance().DisplayImage(new Imager(Constanct.IMAGE_PREFIX + result.logoImageUrl, (ImageView)findViewById(R.id.shops_img)));
		ImageView vip = (ImageView) findViewById(R.id.shops_vip_img);
		if(result.imageUrls != null && result.imageUrls.length > 0 && result.imageUrls[0].length() > 5){
			ArrayList<String> list = new ArrayList<String>();
			for(String url : result.imageUrls){
				if(url.length() > 5){
					list.add(url);
				}
			}
			
			OnScrollListener onScrollListener = new OnScrollListener() {
				@Override
				public void onStartScroll(View v) {
					about.setScrollDisable(v, true);
				}
				
				@Override
				public void onScrollFinish(View v) {
					about.setScrollDisable(null, false);
				}
			};
			
			imagePanel.setVisibility(View.VISIBLE);
			mViewFlow = (ViewFlow) findViewById(R.id.view_flow);
			CircleFlowIndicator indic = (CircleFlowIndicator) findViewById(R.id.viewflowindic);
			indic.setVisibility(View.VISIBLE);
			mViewFlow.setFlowIndicator(indic);
			SupplyImageAdapter adapter = new SupplyImageAdapter(this);
			adapter.setList(list);
			mViewFlow.setAdapter(adapter);
			mViewFlow.setOnScrollListener(mOnScrollListener);
			mViewFlow.setOnScrollListener(onScrollListener);
		}
		if(mItem.level == 2){
			vip.setBackgroundResource(R.drawable.l_1);
		} else if(mItem.level == 3){
			vip.setBackgroundResource(R.drawable.l_2);
		}else if(mItem.level == 4) {
			vip.setBackgroundResource(R.drawable.l_3);
		}
		mItem = result;
	}
	
	private OnScrollListener mOnScrollListener = new OnScrollListener() {
		
		@Override
		public void onStartScroll(View fouceView) {
		}
		
		@Override
		public void onScrollFinish(View fouceView) {
			//mflowCount.setText("0" + "/" + mCount);
		}
	};
	
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int id = v.getId();
		Intent intent = new Intent();
		switch(id){
		case R.id.shop_range_layout:
			if(mItem != null){
				if( mItem.address == null){
					Toast.makeText(this, "没有关于这个商家的地址", Toast.LENGTH_SHORT).show();
					break;
				}
				
				//intent.setClass(getApplicationContext(), GoogleMapActivity.class);
				//intent.setClass(getApplicationContext(), GoogleMapBrowserActivity.class);
				intent.setClass(getApplicationContext(), MapActivity.class);
				intent.putExtra(Constanct.INTENT_TITLE, mItem.shopName);
				intent.putExtra(Constanct.INTENT_LAT, mItem.latitude);
				intent.putExtra(Constanct.INTENT_LON, mItem.longitude);
				intent.putExtra(Constanct.INTENT_ADDR, mItem.address);
				startActivity(intent);
			}
			break;
		case R.id.phone_txt_layout:
			intent.setAction(Intent.ACTION_DIAL);
			intent.setData(Uri.parse("tel:" + ((TextView)findViewById(R.id.phone_txt)).getText().toString()));
			startActivity(intent);
			break;
		case R.id.about_txt:
			findViewById(R.id.about).setVisibility(View.VISIBLE);
			findViewById(R.id.sale_list).setVisibility(View.GONE);
			findViewById(R.id.buy_list).setVisibility(View.GONE);
			findViewById(R.id.about_txt).setBackgroundResource(R.drawable.shops_btn_selected_bg);
			findViewById(R.id.my_product_txt).setBackgroundResource(R.drawable.shops_btn_bg);
			findViewById(R.id.my_buy_txt).setBackgroundResource(R.drawable.shops_btn_bg);
			break;
		case R.id.my_product_txt:
			findViewById(R.id.sale_list).setVisibility(View.VISIBLE);
			findViewById(R.id.about).setVisibility(View.GONE);
			findViewById(R.id.buy_list).setVisibility(View.GONE);
			findViewById(R.id.my_product_txt).setBackgroundResource(R.drawable.shops_btn_selected_bg);
			findViewById(R.id.about_txt).setBackgroundResource(R.drawable.shops_btn_bg);
			findViewById(R.id.my_buy_txt).setBackgroundResource(R.drawable.shops_btn_bg);
			loadBusinessData(BUSINESS_TYPE.SALE_TYPE);
			break;
		case R.id.my_buy_txt:
			findViewById(R.id.buy_list).setVisibility(View.VISIBLE);
			findViewById(R.id.about).setVisibility(View.GONE);
			findViewById(R.id.sale_list).setVisibility(View.GONE);
			findViewById(R.id.my_buy_txt).setBackgroundResource(R.drawable.shops_btn_selected_bg);
			findViewById(R.id.my_product_txt).setBackgroundResource(R.drawable.shops_btn_bg);
			findViewById(R.id.about_txt).setBackgroundResource(R.drawable.shops_btn_bg);
			loadBusinessData(BUSINESS_TYPE.BUY_TYPE);
			break;
		case R.id.title_back:
			finish();
			break;
		case R.id.title_share:
			intent.setClass(this, DetailShareActivity.class);
			startActivity(intent);
			break;
		case R.id.title_collect:
			if(mItem != null){
				LoginResult lr = (LoginResult) DataCache.getInstance().get(Constanct.ISLOGINED_KEY);
				if(lr == null){
					sendBroadcast(new Intent("sw.login"));
					finish();
					break;
				}
				
				CollectorUtils.getInstance().uploadData(new UploadData(mItem.shopId, 4, lr.id, mItem.userId), this);
			}
			break;
		}
	}
	
	private void loadBusinessData(BUSINESS_TYPE type){
		if(mItem == null){
			return;
		}
		switch(type){
		case SALE_TYPE:
			if(mSaleListView == null){
				mSaleListView = (PullRefenceList) findViewById(R.id.sale_list);
				mSaleBusinessAdapter = new BusinessAdapter(type, this);
				mSaleBusinessAdapter.setListView(mSaleListView);
				
				mSaleBusinessAdapter.setMemberID(mItem.userId);
				mSaleBusinessAdapter.setStype(0);
				mSaleBusinessAdapter.moveToFristPage();
				mSaleBusinessAdapter.setShopId(mItem.shopId);
				mSaleBusinessAdapter.loadData();
				mSaleListView.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View v, int index,
							long id) {
						BussnissItem item = (BussnissItem) v.getTag();
						if(item != null){
							Intent intent = new Intent(getApplicationContext(), SupplyDetailsActivity.class);
							intent.putExtra(Constanct.INFO_ENTRY, item);
							
							startActivity(intent);
						} 
					}
				});
			}
			break;
		case BUY_TYPE:
			if(mBuyListView == null){
				mBuyListView = (PullRefenceList) findViewById(R.id.buy_list);
				mBuyBusinessAdapter = new BusinessAdapter(type, this);
				mBuyBusinessAdapter.setListView(mBuyListView);
				mBuyBusinessAdapter.setMemberID(mItem.userId);
				mBuyBusinessAdapter.setStype(0);
				mBuyBusinessAdapter.moveToFristPage();
				mBuyBusinessAdapter.setShopId(mItem.shopId);
				mBuyBusinessAdapter.loadData();
				mBuyListView.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View v, int index,
							long id) {
						BussnissItem item = (BussnissItem) v.getTag();
						if(item != null){
							Intent intent = new Intent(getApplicationContext(), BuyDetailsActivity.class);
							intent.putExtra(Constanct.INFO_ENTRY, item);
							startActivity(intent);
						} 
					}
				});
			}
			break;
		}
	}

}
